package algorithm.leetcode;

import java.util.Arrays;

public class Num88 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 3, 0, 0, 0};
        int m = 3;
        int[] nums2 = {2, 5, 6};
        int n = 3;
        merge(nums1, m, nums2, n);
        Arrays.stream(nums1).forEach(System.out::println);
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int mIndex = m - 1;
        int nIndex = n - 1;
        int tail = m + n - 1;
        int cur;
        while (mIndex >= 0 || nIndex >= 0) {
            if (mIndex == -1) {
                cur = nums2[nIndex--];
            } else if (nIndex == -1) {
                cur = nums1[mIndex--];
            } else if (nums1[mIndex] > nums2[nIndex]) {
                cur = nums1[mIndex--];
            } else {
                cur = nums2[nIndex--];
            }
            nums1[tail--] = cur;
        }
    }
}
